package com.cndw.kungfu.model.goods.app;

import java.util.Map;

import com.cndw.kungfu.config.ConstantsChat;
import com.cndw.kungfu.config.ConstantsIntKey;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.exception.ErrPopException;
import com.cndw.kungfu.exception.SysErrException;
import com.cndw.kungfu.ext.LangUtil;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.sys.ChatMain;

/**
 * vip卡的使用
 * 
 * @author fantadust [9677889@qq.com]
 * @date 2011-9-13 下午3:00:05
 */
public class AppDo104 extends AppBase {

	@Override
	public boolean check() throws Exception {
		if (goodsMode.getValueA() <= 0 || goodsMode.getValueB() <= 0) {
			throw new SysErrException();
		}
		if (goodsMode.getValueA() < player.getKnightTrueLevel()) {
			throw new ErrPopException(LanguageLib.get("goods.use.knightlv"));
		}

		return true;
	}

	@Override
	public Map<String, Object> run() throws Exception {

		int knightLevel = goodsMode.getValueA();
		int knightLifeTime = goodsMode.getValueB();

		int knightExpiredTime = 0;
		int currentTime = TimeProcesser.getUnixTime();
		if (player.getKnightLevel() == knightLevel && player.getKnightExpired() > currentTime) {
			knightExpiredTime = player.getKnightExpired() + knightLifeTime;
		} else {
			knightExpiredTime = currentTime + knightLifeTime;
		}
		player.setKnightLevel(knightLevel);
		player.setKnightExpired(knightExpiredTime);

		if (knightLevel >= 1) {// 黄金卡增加背包个数
			if (player.getHoldGrids() < ConstantsModel.playerGirdsMaxHold || player.getBoxsGirds() < ConstantsModel.playerGirdsMax) {

				int boxsGrids = player.getBoxsGirds();
				int holdGrids = player.getHoldGrids();

				int[] boxsAdd = { 0, 0, 6, 18, 36 };
				int[] holdAdd = { 0, 0, 5, 10, 18 };

				boxsGrids += boxsAdd[knightLevel];
				holdGrids += holdAdd[knightLevel];

				boxsGrids = boxsGrids >= ConstantsModel.playerGirdsMax ? ConstantsModel.playerGirdsMax : boxsGrids;
				holdGrids = holdGrids >= ConstantsModel.playerGirdsMaxHold ? ConstantsModel.playerGirdsMaxHold : holdGrids;

				player.getPlayerBoxs().activationGirds(boxsGrids, holdGrids);
				player.setBoxsGirds(boxsGrids);
				player.setHoldGrids(holdGrids);

				msg.put("playerBoxsGirds", boxsGrids);
				msg.put("playerHoldGirds", holdGrids);

			}
			if (4 == knightLevel) {// vip任务情况会刷新
				Context.getTaskServer().autoRaidioTask(playerId, 1);
			}

		}
		if (knightLevel == 4 && player.getDataInfo().intDataGet(ConstantsIntKey.firstKnight) == 0 ) {
			player.getDataInfo().intDataSet(ConstantsIntKey.firstKnight, 1);
			Context.getGoodsServer().giveGoodsBing(playerId, 102101, 1, 1);
		}

		if (knightLevel > 0) {
			//ChatMain.hearsayMessage(LanguageLib.get("goods.knight.msg", player.getPlayerName(), goodsMode.getName()), ConstantsChat.cwVipBuy);
			ChatMain.hearsayMessage(LanguageLib.get("goods.knight.msg", LangUtil.makeNameLink(player), LangUtil.makeHtml(goodsMode.getColor(), goodsMode.getName())), ConstantsChat.cwVipBuy);
		}

		Context.getPlayerServer().clacProperties(player);
		player.commitSync();

		msg.put("player", player.toPlayerVO());
		ticpString = LanguageLib.get("goods.use.suc");
		return msg;
	}

}
